/**
 * Created by afer on 2017/7/5.
 */


var JsonModel = require('../config/jsonModel');
var ConinsDesDao = require('../Dao/coindesdao');

var multiparty = require('multiparty');
var util = require('util');
var fs = require('fs');
var uploadCoinsImage = require('./uploadImage').uploadCoinsImage;
var Path = require('path');


module.exports = function (app) {

    app.post('/test', function (req, res) {
        uploadCoinsImage(req, res, function (path, fields) {
            console.log(path);

            return res.send('1211212121212' + path);
        });
    });

    app.post('/addCoinDes', function (req, res) {
        uploadCoinsImage(req, res, function (path, fields) {
            var coninname = fields.coinname[0];
            var coindes = fields.coindes[0];
            var model = new JsonModel.JsonModel();
            model.status = 0;
            try {
                if (!coninname) {
                    throw  new Error('coninname 不能为空！');
                }
                if (!coindes) {
                    throw  new Error('coindes 不能为空！');
                }
            } catch (error) {
                model.content = {};
                model.msg = error.message;
                return res.send(model);
            }
            var coinDes = {
                coinname: coninname,
                coindes: coindes,
                coinimage: path.replace(Path.resolve(__dirname, '..') + '/client', '')
            };
            ConinsDesDao.create(coinDes).then(function (result) {
                var ss = result.ops[0];
                if (!ss) {
                    fs.unlinkSync(path); //删除文件
                    model.content = {};
                    model.msg = '创建失败!';
                    model.status = 0;
                    return res.send(model);
                }
                model.content = ss;
                model.status = 1;
                model.msg = '添加成功!';
                return res.send(model);
            }).catch(function (error) {
                console.log(error);
                fs.unlinkSync(path); //删除文件
                model.content = {};
                if (error.message.match('E11000 duplicate key')) {
                    model.msg = '币种重复!';
                } else {
                    model.msg = error.message;
                }
                model.status = 0;
                return res.send(model);
            });
        });
    });

    app.get('/getAllCoinsDes', function (req, res) {
        ConinsDesDao.getAllCoinsDes().then(function (coinsDes) {
            var model = new JsonModel.JsonModel();
            model.status = 1;
            model.content = {coinsDesList: coinsDes};
            return res.send(model);
        });
    });

    app.get('/getCoinDesByName', function (req, res) {
        var name = req.query.name;
        var model = new JsonModel.JsonModel();
        model.status = 0;
        try {
            if (!name) {
                throw  new Error('name 不能为空！');
            }
        } catch (error) {
            model.msg = error.message;
            return res.send(model);
        }
        ConinsDesDao.getCoinDesByName(name).then(function (result) {
            var coindes = result[0];
            if (!coindes) {
                model.msg = '没有该种币的介绍！';
                model.content = {};
                return res.send(model);
            }
            model.msg = '成功！';
            model.content = coindes;
            model.status = 1;
            return res.send(model);
        });
    });

    app.get('/getCoinDesByID', function (req, res) {
        var ID = req.query.id;
        var model = new JsonModel.JsonModel();
        model.status = 0;
        try {
            if (!ID) {
                throw  new Error('id 不能为空！');
            }
        } catch (error) {
            model.msg = error.message;
            return res.send(model);
        }
        ConinsDesDao.getCoinDesByID(ID).then(function (result) {
            var coindes = result[0];
            if (!coindes) {
                model.msg = '没有该种币的介绍！';
                model.content = {};
                return res.send(model);
            }
            model.msg = '成功！';
            model.content = coindes;
            model.status = 1;
            return res.send(model);
        });
    });
}